package org.jeecg.modules.sggg.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.sggg.entity.SgBlutdruckRecord;
import org.jeecg.modules.sggg.entity.SgTreatmentLog;
import org.jeecg.modules.sggg.service.ISgTreatmentLogService;
import org.jeecg.modules.system.controller.SysRoleController;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysRole;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysRoleService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 转诊记录表
 * @Author: jeecg-boot
 * @Date: 2022-11-10
 * @Version: V1.0
 */
@Api(tags = "转诊记录表")
@RestController
@RequestMapping("/sggg/sgTreatmentLog")
@Slf4j
public class SgTreatmentLogController extends JeecgController<SgTreatmentLog, ISgTreatmentLogService> {
    @Autowired
    private ISgTreatmentLogService sgTreatmentLogService;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysDepartService sysDepartController;

    @ApiOperation(value = "转诊记录表-分页列表查询", notes = "转诊记录表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<SgTreatmentLog>> queryPageList(SgTreatmentLog sgTreatmentLog,
                                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SgTreatmentLog> page = new Page<>(pageNo, pageSize);
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //SysRole role = sysRoleController.getUserRole(sysUser.getId()).getResult();
        List<SysRole> roleList = sysRoleService.getUserRole(sysUser.getId());
        String code = "";
        StringBuilder sb = new StringBuilder();
        for (SysRole role : roleList) {
            sb.append(role.getRoleCode());
            sb.append(",");
        }
        String sbstr = sb.toString();
        if (sbstr.contains("admin") ) {
            code = "admin";
        } else if (sbstr.contains("dept_manage")) {
            //机构管理员
            code = "dept_manage";
        } else {
            code = "other";
        }

        SysDepart sysDepart = sysDepartController.getById(sysUser.getDepartIds());
        SysUser sysUser1 = sysUserService.getById(sysUser.getId());
        if(sysDepart !=null && sysDepart.getDeptAttr()!=null){
            // 1.三高之家
            if(sysDepart.getDeptAttr().equals("1")){
//                queryWrapper.eq("doctor_id", sysUser1.getId().toString());
                sysUser.setId(sysUser1.getId());
            }
            // 2.三高基地
            if(sysDepart.getDeptAttr().equals("2")){
                String[] array = sysUser1.getResponsibleAgency().split(",");
                List<String> responsibleAgencies = Arrays.asList(array);
                sgTreatmentLog.setResponsibleAgencies(responsibleAgencies);
//                queryWrapper.in("sys_org_code", responsibleAgencies);
            }
            // 3.三高中心
            if(sysDepart.getDeptAttr().equals("3")){
                String[] array = sysUser1.getResponsibleAgency().split(",");
                List<String> responsibleAgencies = Arrays.asList(array);
                sgTreatmentLog.setResponsibleAgencies(responsibleAgencies);
            }
        }else{
            sysUser.setId(sysUser1.getId());
        }

        IPage<SgTreatmentLog> pageList = sgTreatmentLogService.queryList(page, sgTreatmentLog, sysUser.getId(), code, sysUser.getOrgCode());
        return Result.OK(pageList);
    }

    @AutoLog(value = "转诊记录表-添加")
    @ApiOperation(value = "转诊记录表-添加", notes = "转诊记录表-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SgTreatmentLog sgTreatmentLog) {
        sgTreatmentLogService.save(sgTreatmentLog);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "转诊记录表-编辑")
    @ApiOperation(value = "转诊记录表-编辑", notes = "转诊记录表-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody SgTreatmentLog sgTreatmentLog) {
        sgTreatmentLogService.updateById(sgTreatmentLog);
        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "转诊记录表-通过id删除")
    @ApiOperation(value = "转诊记录表-通过id删除", notes = "转诊记录表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id") String id) {
        sgTreatmentLogService.removeById(id);
        return Result.OK("删除成功!");
    }

    @AutoLog(value = "转诊记录表-批量删除")
    @ApiOperation(value = "转诊记录表-批量删除", notes = "转诊记录表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids") String ids) {
        this.sgTreatmentLogService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }


    @ApiOperation(value = "转诊记录表-通过id查询", notes = "转诊记录表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<SgTreatmentLog> queryById(@RequestParam(name = "id") String id) {
        SgTreatmentLog sgTreatmentLog = sgTreatmentLogService.getById(id);
        if (sgTreatmentLog == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(sgTreatmentLog);
    }

    /**
     * 导出excel
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SgTreatmentLog sgTreatmentLog) {
        return super.exportXls(request, sgTreatmentLog, SgTreatmentLog.class, "转诊记录表");
    }

    /**
     * 通过excel导入数据
     */
    //@RequiresPermissions("sg_treatment_log:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SgTreatmentLog.class);
    }


    /**
     * 转诊记录表-转诊记录查询
     */
    @ApiOperation(value = "转诊记录表-转诊记录查询", notes = "转诊记录表-转诊记录查询")
    @GetMapping(value = "/queryByAssistId")
    public Result<List<SgTreatmentLog>> queryByAssistId(String assistId) {
        List<SgTreatmentLog> list = this.sgTreatmentLogService.queryByAssistId(assistId);
        return Result.OK(list);
    }

    @ApiOperation(value = "查询居民协诊记录列表", notes = "查询居民协诊记录列表")
    @GetMapping(value = "/queryByMemberId")
    public Result<List<SgTreatmentLog>> queryByMemberId(@RequestParam(name = "member_id") String member_id) {
        List<SgTreatmentLog> sgBlutdruckRecord = sgTreatmentLogService.selectByMemberId(member_id);
        if (sgBlutdruckRecord == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(sgBlutdruckRecord);
    }
}
